const {
    getById,        // 根据 id 查询权限     
    getList,        // 获取权限列表     
    create,         // 添加权限     
    edit,           // 编辑权限 
    remove,         // 删除权限 
    getName,         // 只获取权限id 权限名称
} = require("../../service/user/rule")


exports.ruleApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/");
    let urlKey = url[2].split("?")[0]
    const { id } = ctx.params || "";
    const { pageNum, pageSize } = ctx.query || "";
    const { rulename, summary, creator, updator } = ctx.request.body || "";
    let res;
    switch (ctx.request.method) {
        case "GET": if (urlKey == "byid") {
            res = await getById({ id })
        } else if (urlKey == "list") {
            res = await getList({ pageNum, pageSize })
        } else if(urlKey == "onlyname"){
            res = await getName()
        }; break;
        case "POST": if(urlKey == "create"){res = await create({ rulename, summary, creator })}; break;
        case "PUT": res = await edit({ rulename, summary, updator, id }); break;
        case "DELETE": res = await remove({ id }); break;
        default: res = { msg: "操作失败", code: "400" }
    }
    ctx.body = res
}